from random import random

from cal_time import *


# 二分法查找
@cal_time
def binary_search(li, val):
    """
    二分法查找
    :param li: 查找的数据源，如：数组，元组
    :param val:查找的值
    :return: int 整形的位置信息，查找失败返回-1
    """
    left = 0
    right = len(li) - 1
    while left <= right:  # 如果候选区有值

        mid = (left + right) // 2  # 整除2，取【left】和【right】的中间值

        if li[mid] == val:  # 找到查找结果
            return mid
        elif li[mid] > val:  # 待查找的值在mid左侧
            right = mid - 1
        else:  # 待查找的值在mid右侧
            left = mid + 1
    else:
        return -1


if __name__ == '__main__':
    li = list(range(10000000000))
    binary_search(li, 1000000)


'''
时间复杂度 O(log2n)
'''